package com.aspire.moss.portal.desktop;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.graphics.Bitmap;

/**
 * 保存一个应用的详细信息
 * @author Administrator
 *
 */
public class AppInfo extends ItemInfo implements Cloneable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 图标
	 */
	public Bitmap iconBitmap;
	
	/**
	 * 标题
	 */
	public String title;
	
	/**
	 * 是否已安装 , 是否有更新
	 */
	public boolean isInstall ,isUpdate ,isDock;
	
	/**
	 * 包名
	 */
	public String packageName;

	public AppInfo(Bitmap iconBitmap, String title) {
		super();
		this.iconBitmap = iconBitmap;
		this.title = title;
	}
	
	public AppInfo(int screen ,int cellX, int cellY , Bitmap iconBitmap, String title , String packageName) {
		super(screen ,cellX , cellY);
		this.iconBitmap = iconBitmap;
		this.title = title;
		this.packageName = packageName;
	}
	
	public AppInfo(int screen ,int cellX, int cellY , String title , String packageName) {
		super(screen ,cellX , cellY);
		this.title = title;
		this.packageName = packageName;
	}
	
	public AppInfo(Bitmap iconBitmap, String title, boolean isInstall,
			boolean isUpdate, boolean isDock, String packageName) {
		super();
		this.iconBitmap = iconBitmap;
		this.title = title;
		this.isInstall = isInstall;
		this.isUpdate = isUpdate;
		this.isDock = isDock;
		this.packageName = packageName;
	}

	public AppInfo(int screen ,int cellX, int cellY , String title, boolean isInstall,
			boolean isUpdate, String packageName) {
		super(screen ,cellX , cellY);
		this.title = title;
		this.isInstall = isInstall;
		this.isUpdate = isUpdate;
		this.packageName = packageName;
	}

	/**
	 * 比较两个对象是否在同一个位置
	 * @param appInfo
	 * @return
	 */
	public boolean eqXYScreen(AppInfo appInfo) {
		if (this.cellX == appInfo.cellX && this.cellY == appInfo.cellY
				&& this.screen == appInfo.screen){
			return true;
		}
		return false;
	}
	
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		final AppInfo appInfo = (AppInfo) super.clone();
		appInfo.screen = screen;
		appInfo.cellX = cellX;
		appInfo.cellY = cellY;
		appInfo.iconBitmap = iconBitmap;
		appInfo.isInstall = isInstall;
		appInfo.isUpdate= isUpdate;
		appInfo.title= title;
		appInfo.packageName = packageName;
		return appInfo;
	}
	
	public AppInfo cloneAppInfo(){
		try {
			return  (AppInfo) this.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String toString() {
		return super.toString() 
			+ " iconBitmap=" + iconBitmap
			+ " title=" + title
			+ " isInstall=" + isInstall
			+ " isUpdate=" + isUpdate;
	}
	
	public static String appInfoToJson(AppInfo info){
		StringBuffer buffer = new StringBuffer();
		buffer.append("{");
		buffer.append("\"screen\":" + info.screen + ",");
		buffer.append("\"cellX\":" + info.cellX + ",");
		buffer.append("\"cellY\":" + info.cellY + ",");
		buffer.append("\"title\":" + "\"" +info.title + "\",");
		buffer.append("\"packageName\":" + "\"" +info.packageName + "\",");
		buffer.append("\"isInstall\":" + "\"" +info.isInstall + "\",");
		buffer.append("\"isUpdate\":" + "\"" +info.isUpdate + "\"");
		buffer.append("}");
		return buffer.toString();
	}
	
	public String appInfoToJson(){
		return appInfoToJson(this);
	}
	
	public static AppInfo jsonToAppInfo(String json){
		try {
			final  JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
			final int screen = object.getInt("screen");
			final int cellX = object.getInt("cellX");
			final int cellY = object.getInt("cellY");
			final String title = object.getString("title");
			final String packageName = object.getString("packageName");
			final boolean isInstall = object.getBoolean("isInstall");
			final boolean isUpdate = object.getBoolean("isUpdate");
			return new AppInfo(screen , cellX , cellY , title , isInstall , isUpdate , packageName);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
}
